import { defineStore } from "pinia";
import http from "../utils/request.js";
import { wsManager } from "../utils/wsManager";
import { useMenusStore } from "./menus";
import { ElMessageBox, ElMessage } from "element-plus";
import { router } from "../router/index";
export const useAccountStore = defineStore("account", {
    state: () => {
        return {
            token: "",
            uid: "",
            username: "",
            avatar: "",
            isShowUnauthorizedAlert: false, //控制弹框仅显示一次
            accountGroupList: [], //管理员角色组列表
        };
    },
    persist: {
        omit: ["isShowUnauthorizedAlert"],
    },
    getters: {
        isLogin: (state) => {
            return !!state.token;
        },
    },
    actions: {
        //登录
        login(username = "", password = "") {
            if (!username || !password) {
                let errMsg = "用户名或密码不能为空";
                return Promise.reject({
                    message: errMsg,
                });
            }
            let res = http.post("/sunAdmin/account/login", {
                username: username,
                password: password,
            });
            res.then((res) => {
                if (res.code !== 200) {
                    return;
                }
                this.token = res.data.token;
                this.accountAuthSuccess(res.data);
                //连接ws
                wsManager.connect();
            });
            return res;
        },
        //退出登录
        async loginOut() {
            //关闭ws
            wsManager.disconnect();
            await http.get("/sunAdmin/account/loginOut");
            //登录过期逻辑
            this.token = "";
            //跳转登录
            router.push("/login");
        },
        //管理员身份认证成功后
        accountAuthSuccess(accountInfo = {}) {
            this.uid = accountInfo.uid;
            this.username = accountInfo.username;
            this.avatar = accountInfo.avatar;
            //处理导航菜单
            useMenusStore().handleAccountMenus(accountInfo.menusList);
        },
        //服务器token验证
        async tokenAuth(id = null) {
            try {
                let res = await http.get("/sunAdmin/account/tokenAuth");
                if (res.code !== 200) {
                    throw new Error(res.message);
                }
                this.accountAuthSuccess(res.data);
                return res;
            } catch (err) {
                console.error("服务器token鉴权失败", err.message);
                return err;
            }
        },
        //认证失败
        tokenAuthFail(message = "登录失效,请重新登录") {
            if (!this.isShowUnauthorizedAlert) {
                this.isShowUnauthorizedAlert = true;
                ElMessageBox.alert(message, "提示", {
                    confirmButtonText: "确定",
                    callback: (action) => {
                        this.isShowUnauthorizedAlert = false;
                        this.loginOut();
                    },
                });
            }
        },
        //获取管理员角色组
        getGroupList() {
            return http.get("/sunAdmin/account/getGroupList");
        },
        //创建编辑角色组
        editAccountGroup(data) {
            return http.post("/sunAdmin/account/editAccountGroup", data);
        },
        //删除角色组
        deleteAccountGroup(groupId) {
            return http.get("/sunAdmin/account/deleteAccountGroup/" + groupId);
        },
        //获取管理员账号列表
        getAccountList() {
            return http.get("/sunAdmin/account/getAccountList");
        },
        //新增 编辑管理员账号
        editAccount(data) {
            return http.post("/sunAdmin/account/editAccount", data);
        },
        //删除管理员账号
        delAccount(accountId) {
            return http.get("/sunAdmin/account/delAccount/" + accountId);
        },
        //强制踢下线
        accountOffLine(accountId) {
            return http.get("/sunAdmin/account/accountOffLine/" + accountId);
        },
    },
});
